{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2ba1650a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import nltk,re,pprint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "1d38f9cc",
   "metadata": {},
   "outputs": [
    {
     "ename": "LookupError",
     "evalue": "\n**********************************************************************\n  Resource \u001b[93m\u001b[0m not found.\n  Please use the NLTK Downloader to obtain the resource:\n\n  \u001b[31m>>> import nltk\n  >>> nltk.download('')\n  \u001b[0m\n  For more information see: https://www.nltk.org/data.html\n\n  Attempted to load \u001b[93m./\u001b[0m\n\n  Searched in:\n    - 'C:\\\\Users\\\\13917/nltk_data'\n    - 'C:\\\\ProgramData\\\\Anaconda3\\\\nltk_data'\n    - 'C:\\\\ProgramData\\\\Anaconda3\\\\share\\\\nltk_data'\n    - 'C:\\\\ProgramData\\\\Anaconda3\\\\lib\\\\nltk_data'\n    - 'C:\\\\Users\\\\13917\\\\AppData\\\\Roaming\\\\nltk_data'\n    - 'C:\\\\nltk_data'\n    - 'D:\\\\nltk_data'\n    - 'E:\\\\nltk_data'\n**********************************************************************\n",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mLookupError\u001b[0m                               Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-2-18928d2bcc84>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mnltk\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfind\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'.'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\nltk\\data.py\u001b[0m in \u001b[0;36mfind\u001b[1;34m(resource_name, paths)\u001b[0m\n\u001b[0;32m    581\u001b[0m     \u001b[0msep\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m\"*\"\u001b[0m \u001b[1;33m*\u001b[0m \u001b[1;36m70\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    582\u001b[0m     \u001b[0mresource_not_found\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m\"\\n%s\\n%s\\n%s\\n\"\u001b[0m \u001b[1;33m%\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0msep\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmsg\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msep\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 583\u001b[1;33m     \u001b[1;32mraise\u001b[0m \u001b[0mLookupError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mresource_not_found\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    584\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    585\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mLookupError\u001b[0m: \n**********************************************************************\n  Resource \u001b[93m\u001b[0m not found.\n  Please use the NLTK Downloader to obtain the resource:\n\n  \u001b[31m>>> import nltk\n  >>> nltk.download('')\n  \u001b[0m\n  For more information see: https://www.nltk.org/data.html\n\n  Attempted to load \u001b[93m./\u001b[0m\n\n  Searched in:\n    - 'C:\\\\Users\\\\13917/nltk_data'\n    - 'C:\\\\ProgramData\\\\Anaconda3\\\\nltk_data'\n    - 'C:\\\\ProgramData\\\\Anaconda3\\\\share\\\\nltk_data'\n    - 'C:\\\\ProgramData\\\\Anaconda3\\\\lib\\\\nltk_data'\n    - 'C:\\\\Users\\\\13917\\\\AppData\\\\Roaming\\\\nltk_data'\n    - 'C:\\\\nltk_data'\n    - 'D:\\\\nltk_data'\n    - 'E:\\\\nltk_data'\n**********************************************************************\n"
     ]
    }
   ],
   "source": [
    "nltk.find('.')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "5085159b",
   "metadata": {},
   "outputs": [],
   "source": [
    "#f = open('/home/a/Desktop/Projects/FinnegansWake/JamesJoyce-FinnegansWake.txt')\n",
    "raw = 'dsfasdfoi is a test! asdfijdfke, safasdfa, are your ok?'\n",
    "tokens = nltk.wordpunct_tokenize(raw)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "5a96be28",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['dsfasdfoi',\n",
       " 'is',\n",
       " 'a',\n",
       " 'test',\n",
       " '!',\n",
       " 'asdfijdfke',\n",
       " ',',\n",
       " 'safasdfa',\n",
       " ',',\n",
       " 'are',\n",
       " 'your',\n",
       " 'ok',\n",
       " '?']"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tokens"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "6097c7dd",
   "metadata": {},
   "outputs": [],
   "source": [
    "text = nltk.Text(tokens)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "e0d84978",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Text: dsfasdfoi is a test ! asdfijdfke , safasdfa...>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "02570545",
   "metadata": {},
   "outputs": [],
   "source": [
    "words = [w.lower() for w in text]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "8974a9fa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['dsfasdfoi',\n",
       " 'is',\n",
       " 'a',\n",
       " 'test',\n",
       " '!',\n",
       " 'asdfijdfke',\n",
       " ',',\n",
       " 'safasdfa',\n",
       " ',',\n",
       " 'are',\n",
       " 'your',\n",
       " 'ok',\n",
       " '?']"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "words"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "77a49cbe",
   "metadata": {},
   "outputs": [],
   "source": [
    "englishraw =  'dsfasdfoi is a test! asdfijdfke, safasdfa, are your ok?'\n",
    "englishtokens = nltk.wordpunct_tokenize(englishraw)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "5443bdfc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['dsfasdfoi',\n",
       " 'is',\n",
       " 'a',\n",
       " 'test',\n",
       " '!',\n",
       " 'asdfijdfke',\n",
       " ',',\n",
       " 'safasdfa',\n",
       " ',',\n",
       " 'are',\n",
       " 'your',\n",
       " 'ok',\n",
       " '?']"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "englishtokens"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "8a0afd98",
   "metadata": {},
   "outputs": [],
   "source": [
    "englishtext = nltk.Text(englishtokens)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "093ab08e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Text: dsfasdfoi is a test ! asdfijdfke , safasdfa...>"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "englishtext"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "7d861ef3",
   "metadata": {},
   "outputs": [],
   "source": [
    "englishwords = [w.lower() for w in englishtext]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "879e0f78",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['dsfasdfoi',\n",
       " 'is',\n",
       " 'a',\n",
       " 'test',\n",
       " '!',\n",
       " 'asdfijdfke',\n",
       " ',',\n",
       " 'safasdfa',\n",
       " ',',\n",
       " 'are',\n",
       " 'your',\n",
       " 'ok',\n",
       " '?']"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "englishwords"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "cbf6ecb8",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'vocab' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-17-137ba2dd67e3>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mset\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mvocab\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'vocab' is not defined"
     ]
    }
   ],
   "source": [
    "set(vocab)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "4b9cc045",
   "metadata": {},
   "outputs": [],
   "source": [
    "from nltk.corpus import wordnet\n",
    "\n",
    "englishwords = [w.lower() for w in englishtext if wordnet.synsets(w.lower())]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "ab720abc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['is', 'a', 'test', 'are', 'ok']"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "englishwords"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bcd9fd09",
   "metadata": {},
   "outputs": [],
   "source": [
    "if not wordnet.synsets(word_to_test):\n",
    "    #Not an English Word\n",
    "else:\n",
    "    #English Word如果您在安装wordnet时遇到问题或想尝试其他方法，请参阅本文。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
